package com.antdesignbackend.backend.form.approvalOpinion.controller;

import java.io.Serializable;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.form.approvalOpinion.entity.ApprovalOpinion;
import com.antdesignbackend.backend.form.approvalOpinion.entity.entityVo.ApprovalOpinionVo;
import com.antdesignbackend.backend.form.approvalOpinion.service.ApprovalOpinionService;

/**
 * 审批意见设置
 *
 * @author 林宇杰
 * @email approvalOpinion
 * @date 2020-08-26 13:58:55
 */

@Controller
@RequestMapping("/approvalOpinion")
@Slf4j
public class ApprovalOpinionController extends BaseController{

	private static final String FUN_NAME = "审批意见设置";//功能名称

	@Autowired
	private ApprovalOpinionService approvalOpinionService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "form/approvalOpinion/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody ApprovalOpinionVo approvalOpinionVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<ApprovalOpinion> wrapper = new QueryWrapper<>();
		Page<ApprovalOpinion> page = new Page<>(approvalOpinionVo.getPageIndex(), approvalOpinionVo.getPageSize());
		IPage<ApprovalOpinion> pageResult = approvalOpinionService.page(page, wrapper);
		result.setTotal((int)pageResult.getTotal());
		result.setData(pageResult.getRecords());
		return result;

	}

	@GetMapping("/getById/{iD}")
	@ResponseBody
	public	ApprovalOpinion getById(@PathVariable(name = "iD") String iD) {
		ApprovalOpinion obj = approvalOpinionService.getById(iD);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody ApprovalOpinion approvalOpinion){
		ReturnVO result = new ReturnVO();
		if(approvalOpinionService.save(approvalOpinion)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody ApprovalOpinion approvalOpinion){
		ReturnVO result = new ReturnVO();
		if(approvalOpinionService.updateById(approvalOpinion)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (approvalOpinionService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

	/**
	 * 查询所有
	 */
	@GetMapping("/selectByType")
	@ResponseBody
	public ReturnVO selectByType(@RequestParam(value = "type") String type) {
		ReturnVO result = new ReturnVO();
		QueryWrapper<ApprovalOpinion> wrapper = new QueryWrapper<>();
		wrapper.eq("type",type);
		List<ApprovalOpinion> list = approvalOpinionService.list(wrapper);
		result.setData(list);
		return result;
	}
}
